package lk.icta.core.web.action;

import lk.icta.common.web.action.BaseAction;
import lk.icta.core.entity.AppointmentSchedule;
import lk.icta.core.service.AppointmentScheduleManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Kusal Samarakoon
 */
public class AppointmentScheduleAction extends BaseAction {

    private static final Logger logger = LoggerFactory.getLogger(AppointmentScheduleAction.class);
    private final AppointmentScheduleManagementService appointmentScheduleManagementService;

    private long applicationId;
    private AppointmentSchedule appointmentSchedule;

    public AppointmentScheduleAction(AppointmentScheduleManagementService appointmentScheduleManagementService) {
        this.appointmentScheduleManagementService = appointmentScheduleManagementService;
    }

    public String welcome() {
        try {
            logger.debug("Welcome AppointmentScheduleAction");
            appointmentSchedule = appointmentScheduleManagementService.findScheduleByApplicationId(user, applicationId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    //Save Appointment Schedule Detail
    public String saveAppointmentSchedule() {
        try {
            logger.debug("save AppointmentSchedulel {} - ", user.getUsername());
            if (applicationId != -1) {
                appointmentSchedule.setApplicationId(applicationId);
                appointmentSchedule.setDivisionId(user.getDsDivisions().iterator().next().getId());
                appointmentScheduleManagementService.addAppointmentSchedule(appointmentSchedule, user);
                addActionMessage(getText("successfuly_save_appointment_schedule"));
            } else {
                addActionError(getText("unable_to_appointment_schedule"));
            }
            return SUCCESS;
        } catch (Exception e) {
            logger.error("Error in save Appointment Schedule  : {}", e);
            e.printStackTrace();
            addActionError(getText("unable_to_save_appointment_schedule"));
            return ERROR;
        }

    }

    public long getApplicationId() {
        return applicationId;
    }

    public void setApplicationId(long applicationId) {
        this.applicationId = applicationId;
    }

    public AppointmentSchedule getAppointmentSchedule() {
        return appointmentSchedule;
    }

    public void setAppointmentSchedule(AppointmentSchedule appointmentSchedule) {
        this.appointmentSchedule = appointmentSchedule;
    }
}
